Declaration of a class in ST

Syntax
CLASS (* optional_begin *) FINAL|ABSTRACT (* optional_end *) class_name_2
 
(* optional_begin *) USING Namespace_1;
USING Namespace_2; (* optional_end *)
 
(* optional_begin *) EXTENDS class_name_1 (* optional_end *)
 
(* optional_begin *) IMPLEMENTS interface_1, interface_2, ... interface_n (* optional_end *)
 
(* optional: declaration of variables/instances *)
 
(* optional: declaration of methods *)
 
END_CLASS
Meaning

declaration of a →class a feature of the →object-oriented programming), class_name_2 must be an →IEC-identifier.
The declaration is possible within an ST-object  – the declaration is done either within the global →namespace or within a declared namespace. CLASS and END_CLASS are →keywords for the declaration of the class.

Observe that a class is similar to a →function block that is using the features of the object-oriented programming. According to the →IEC-standard, there are the following differences:

  • The keywords FUNCTION_BLOCK and END_FUNCTION_BLOCK are replaced by CLASS and END_CLASS respectively.

  • Variables are only declared in the VAR section  . The sections VAR_INPUT, VAR_OUTPUT, VAR_IN_OUT, and VAR_TEMP are not allowed.

    According to the IEC-table 48 "Class" (line 4a and 4b), variables are declared in the VAR_EXTERNAL section as well.

  • A class has no body. A class may define only methods.

  • A call of an instance of a class is not possible. Only the methods of a class can be called.

For details on the optional keywords FINAL, ABSTRACT, EXTENDS, IMPLEMENTS, see "Declaration of a function block in ST" for their explanation. Just observe that:

  • EXTENDS derives the class from a different class (this will be the base class).

  • IMPLEMENTS implements one or more →interfaces in the class.

The USING namespace directive after the name of the class is optional as well. See "Namespaces in ST: usage" for details on this directive.

Example 1: Class with methods
Class CCounter
  VAR
  m_iCurrentValue : DINT; (* Default = 0 *)
  m_bCountUp : BOOL:=TRUE;
  END_VAR
  VAR PUBLIC
    m_iUpperLimit : DINT:=+40;
    m_iLowerLimit : DINT:=-40;
  END_VAR
  METHOD PUBLIC Count : DINT
    IF (m_bCountUp AND m_iCurrentValue<m_iUpperLimit) THEN
      m_iCurrentValue:= m_iCurrentValue+1;
    END_IF;
    IF (NOT m_bCountUp AND m_iCurrentValue>m_iLowerLimit) THEN
      m_iCurrentValue:= m_iCurrentValue-1;
    END_IF;
  Count := m_iCurrentValue;
  END_METHOD
  METHOD public SetDirection
    VAR_INPUT
      bCountUp : BOOL;
    END_VAR
    m_bCountUp:=bCountUp;
  END_METHOD
END_CLASS

You are able to use the following language elements for ST within a class:

The usage of these language elements makes it possible to use other elements (e.g. declaration of STRING variables within the section VAR ... END_VAR ) as well. Such elements are not listed here.